package com.fastjars.business.shiro.filter;

import com.fastjars.business.spring.http.Resp;
import org.apache.commons.util.RequestUtils;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * LogoutFilter
 * 退出登录过滤器
 * @author Brant Liu <br>
 * 邮箱：lbf1988@qq.com <br>
 * 日期：2018/5/9
 * @version 1.0.0
 */
public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter{
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        // TODO: 2018/5/9 Ajax请求处理
        if(RequestUtils.isAjaxRequest((HttpServletRequest) request)){
            Subject subject = this.getSubject(request, response);
            subject.logout();
            WriterUtils.write(response, Resp.success().build().toJsonString());
            return false;
        }
        // TODO: 2018/5/9 非AJAX请求
        super.preHandle(request, response);
        return false;
    }
}
